package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DelOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书");

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要删除的书名 ：");
        String name = scanner.nextLine();

        int currentSize = bookList.getUsedSize();

        int index = -1;

        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBooks(i);
            if(book.getName().equals(name)){
                index = i;
                break;
            }
        }
        if(index == -1){
            System.out.println("没有你删除的书");
            return;
        }else{

            for (int i = index; i < currentSize-1; i++) {
                Book book1 = bookList.getBooks(i+1);
                bookList.setBooks(book1,i);
            }

        }
        bookList.setUsedSize(currentSize-1);
    }
}
